<?php
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.phpdoctrine.org>.
 */

/**
 * Doctrine_Comment
 *
 * @package     Doctrine
 * @subpackage  Comment
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link        ~
 * @since       0.1
 * @version     $Revision$
 * @author      Marek Snopkowski <snpy@snpy.info>
 */
class Doctrine_Comment extends Doctrine_Record_Generator
{
  protected $_options = array(
                            'className'     => '%CLASS%Comment',
                            'tableName'     => '%TABLE%_comment',
                            'generateFiles' => false,
//                            'generatePath'  => './lib/model/doctrine/behaviour',
                            'table'         => false,
                            'pluginTable'   => false,
                            'children'      => array(),
                            'cascadeDelete' => true,
                            'appLevelDelete'=> false,

                            'content'      => array(
                                'name'     => 'content',
                                'alias'    => null,
                                'type'     => 'text',
                                'notblank' => true,
                            ),
                            'commenter'    => array(
                                'name'     => 'user_id',
                                'alias'    => null,
                                'type'     => 'integer',
                                'notblank' => true,
                            ),
                            'commentee'    => array(
                                'name'     => 'element_id',
                                'alias'    => null,
                                'type'     => 'integer',
                                'notblank' => true,
                            ),

                            'foreignAlias' => 'Comments',
                            'localAlias'   => null,
  );

  /**
   * __construct
   *
   * @param string $options
   * @return void
   */
  public function __construct($options)
  {
    $this->_options = Doctrine_Lib::arrayDeepMerge($this->_options, $options);
  }

  public function buildRelation()
  {
    $this->buildForeignRelation($this->_options['foreignAlias']);
    $this->buildLocalRelation($this->_options['localAlias']);
  }

  public function getRelationLocalKey()
  {
    return $this->_options['commentee']['name'];
  }

  /**
   * buildDefinition
   *
   * @param object $Doctrine_Table
   * @return void
   */
  public function setTableDefinition()
  {
    if (empty($this->_options['content']) || empty($this->_options['commenter']) || empty($this->_options['commentee'])) {
      throw new Doctrine_Comment_Exception('Comment fields not set (content, commenter, commentee).');
    }

    $columns = array('id' => array('type' => 'integer', 'primary' => true, 'autoincrement' => true));
    $columns[$this->_options['content']['name']]   = $this->_options['content'];
    $columns[$this->_options['commentee']['name']] = $this->_options['commentee'];
    $columns[$this->_options['commenter']['name']] = $this->_options['commenter'];

    $this->hasColumns($columns);

    $this->index('comment_owner_index', array('fields' => array($this->_options['commenter']['name'])));
    $this->index('commented_element_index', array('fields' => array($this->_options['commentee']['name'])));

    $this->bindQueryParts(array('indexBy' => 'id'));
  }

  public function setUp()
  {
    $this->hasOne('sfGuardUser as User', array(
         'local'   => $this->_options['commenter']['name'],
         'foreign' => 'id',
         'foreignKeyName' => 'sf_guard_user_message_comment_fk'));
    $this->hasOne('Profile', array(
         'local'   => $this->_options['commenter']['name'],
         'foreign' => 'user_id',
         'foreignKeyName' => 'profile_message_comment_fk'));
  }
}
